class Gc_HealthView : StaticEventHandler {
	
	// const total_alpha	= 0.5;
	const tics_to_fade	= 7;
	const tics_to_stay	= 25;
	const screen_height	= 640;
	const vertical_pos	= 0.0666;
	
	// Target and viewer info:
	private SpotActor		spot;
	private SpotActor		old_spot;
	private SpotActor		last_valid_spot;
	private PlayerPawn		viewer;
	private bool			allow_through_walls;
	
	// Timing / Faders:
	private FadeTick		alpha_t;
	private FadeTick		health_t;
	private FadeTick		leave_t;
	private bool			leaving;
	
	// Target info:
	private Actor			target_override;
	private Actor			target;
	private int				health;
	private int				max_health;
	private int				tick_speed;
	
	// Draw info:
	private int				draw_x;
	private int				draw_y;
	private int				color_1;
	private int				color_2;
	private double			color_interpol;
	private RenderScreen	rscreen;
	private RenderString	name_str;
	private RenderString	health_str;
	private RedFontStages	health_colors;
	
	// User settings:
	private transient Cvar	cvar_hp_style;
	private transient Cvar	cvar_show;
	private transient Cvar	cvar_slow;
	private bool			shown;
	
	// kd: Since Sandman is now a thing, people might want the target viewer
	// to also agree with its target locator. This function will allow that.
	void OverrideTarget (Actor considered) {
		if(considered) {
			target_override = considered;
		}
	}
	
	override void WorldLoaded (WorldEvent event) {
		InitHealthView();
	}
	
	private void InitHealthView () {
		if(playeringame [consoleplayer]) {
			allow_through_walls	= false;
			PlayerInfo info	= players [consoleplayer];
			viewer			= info.mo;
			color_1			= 0;
			color_2			= 0;
			health			= 0;
			max_health		= 1;
			
			health_colors	= new("RedFontStages");
			health_colors.Init();
			
			cvar_hp_style		= Cvar.GetCvar("gc_hpview_style", info);
			cvar_show			= Cvar.GetCvar("gc_hpview_show", info);
			cvar_slow			= Cvar.GetCvar("gc_hpview_slow", info);
			shown				= cvar_show.GetBool();
			
			InitScreen();
			ResetDrawData();
		}
	}
	
	void AllowThroughWalls (bool mode) {
		allow_through_walls = mode;
	}
	
	private void ResetDrawData () {
		alpha_t.Force(0);
		health_t.Force(0);
		leave_t.Force(0);
		leaving = false;
	}
	
	private void InitScreen () {
		double scale =
			rscreen.HudScale() * rscreen.CustomScale("gc_hpview_scale");
		
		rscreen.ScaleToHeight(screen_height / max(0.1, scale));
		[draw_x, draw_y] = rscreen.NormalToScaled(0.5, vertical_pos);
	}
	
	private void FindTarget () const {
		old_spot.Copy(spot);
		
		// kd: Overrides first.
		if(target_override) {
			spot.OverrideTarget(target_override);
			target_override = NULL;
		}
		
		// Or just find targets normally!
		else {
			/* if(allow_through_walls) {
				spot.UnderCrosshairNoWalls(viewer);
			}
			
			else {
				spot.UnderCrosshair(viewer);
			} */
			
			spot.UnderCrosshair(viewer);
		
			// kd: We'll check certain stats here now. If there's something you
			// don't want the health viewer to pick up, do it here.
			let mo = spot.Get();
			
			if(mo) {
				bool allies_only = CvarEx.GetBoolDef(
					"gc_hpview_alliesonly", viewer.player, false);
				
				// kd: We get renderstyle == 0 if it's invisible, 4 if it's fuzzy.
				if((allies_only && !mo.IsFriend(viewer)) ||
					mo.health < 1 ||
					mo.binvisible || ( (
					mo.GetRenderStyle() == 0 ||
					mo.GetRenderStyle() == 4 ||
					mo.cursector.lightlevel < 60 || (
					mo.bstealth && mo.alpha < 0.1) ) &&
					viewer.CountInv("PowerVeilVision") < 1)) {
					spot.Discard();
				}
			}
		}
	}
	
	override void WorldTick () {
		shown = cvar_show.GetBool();
		
		if(!shown) {
			return;
		}
		
		FindTarget();
		
		if(spot.HasSpotted(old_spot)) {
			InitScreen();
			CacheTargetInfo();
			alpha_t.ContinueTo(tics_to_fade);
			leaving = false;
		}
		
		else if(spot.HasChanged(old_spot)) {
			last_valid_spot.Copy(old_spot);
			CacheTargetInfo();
		}
		
		else if(spot.HasLeft(old_spot)) {
			last_valid_spot.Copy(old_spot);
			leave_t.To(tics_to_stay);
			leaving = true;
		}
			
		if(target) {
			health = max(0, target.health);
		}
		
		if(leave_t.IsDone() && leaving) {
			alpha_t.ContinueTo(0);
			leaving = false;
		}
		
		leave_t.Next();
		NextColors();
		MakeHealthString();
	}
	
	override void RenderOverlay (RenderEvent event) {
		if(alpha_t.Now() < 1 || !shown) {
			return;
		}
		
		// let alpha = alpha_t.RenderStandardDelta(7, event.fractic) * total_alpha;
		let alpha = alpha_t.RenderStandardDelta(tics_to_fade, event.fractic);
		
		Screen.DrawText(smallfont, Font.cr_red,
			draw_x + name_str.x_offset, draw_y,
			name_str.text,
			DTA_ALPHA, alpha,
			DTA_VIRTUALWIDTHF, rscreen.width,
			DTA_VIRTUALHEIGHTF, rscreen.height,
			DTA_KEEPRATIO, true);
		Screen.DrawText(smallfont, color_1,
			draw_x + health_str.x_offset, draw_y + 8,
			health_str.text,
			DTA_ALPHA, alpha,
			DTA_VIRTUALWIDTHF, rscreen.width,
			DTA_VIRTUALHEIGHTF, rscreen.height,
			DTA_KEEPRATIO, true);
		Screen.DrawText(smallfont, color_2,
			draw_x + health_str.x_offset, draw_y + 8,
			health_str.text,
			DTA_ALPHA, color_interpol * alpha,
			DTA_VIRTUALWIDTHF, rscreen.width,
			DTA_VIRTUALHEIGHTF, rscreen.height,
			DTA_KEEPRATIO, true);
	}
	
	private void CacheTargetInfo () {
		target		= spot.Get();
		max_health	= max(1, target.SpawnHealth());
		tick_speed	= max(1, max_health / 100);
		
		String name_tag = ActorEx.BetterTag(target);
		
		if(target.IsFriend(viewer)) {
			name_tag = name_tag .. "\c[green] (Ally)";
		}
		
		if(target.bdormant) {
			name_tag = name_tag .. "\c[ice] (Statue)";
		}
		
		if(target.binvulnerable) {
			name_tag = name_tag .. "\c[yellow] (Godmode)";
		}
		
		if(target.GetRenderStyle() == 4) {
			name_tag = name_tag .. "\c[grey] (Fuzzy)";
		}
		
		if(target.GetRenderStyle() == 0 || target.binvisible) {
			name_tag = name_tag .. "\c[white] (Invisible)";
		}
		
		if(target.bstealth) {
			name_tag = name_tag .. "\c[darkgrey] (Stealth)";
		}
		
		name_str.Set(name_tag, smallfont, 0.5);
		
		// kd: This allows the dropping animation to continue if you set your
		// sights back on the same target.
		if(last_valid_spot.HasChanged(spot) || !last_valid_spot.IsValid()) {
			health_t.Force(target.health);
		}
	}
	
	private void NextColors () {
		if(cvar_slow.GetBool()) {
			health_t.ContinueTo(health);
		}
		
		else {
			health_t.Force(health);
		}
		
		health_t.NextStep(tick_speed);
		alpha_t.Next();
		
		[color_1, color_2, color_interpol] =
			health_colors.Get(health_t.Now(), max_health);
	}
	
	private void CacheHealthPercentage () const {
		health_str.Set(
			String.Format("%02.1f%%", 100.0 * health_t.Now() / max_health),
			smallfont, 0.5);
	}
	
	private void CacheHealthPoints () const {
		health_str.Set(
			String.Format("%iHP", health_t.Now()), smallfont, 0.5);
	}
	
	private void MakeHealthString () const {
		if(0 < cvar_hp_style.GetInt()) {
			CacheHealthPoints();
		}
		
		else {
			CacheHealthPercentage();
		}
	}
}